package com.visitor.web.controller;

import com.github.pagehelper.PageInfo;
import com.visitor.bean.SmartUser;
import com.visitor.service.SmartUserService;
import com.visitor.web.vm.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Api(tags="用户模块")
@Controller
@RequestMapping(value = "userManager")
public class SmartUserController {
	@Autowired
	private SmartUserService smartUserService;

	@ApiOperation(value="添加用户",notes="添加用户")
	@ResponseBody
	@PostMapping("/addUser")
	public Response<String> addUser(SmartUser user){
		return smartUserService.saveOrUpdate(user);
	}

	@ApiOperation(value="按照编号删除用户信息",notes="按照编号删除用户信息")
	@ApiImplicitParam(name="id",value="用户编号",paramType="path",required=true)
	@ResponseBody
	@PostMapping("/delUser/{id}")
	public Response<String> delUser(@PathVariable("id")Long id){
		smartUserService.deleteUserById(id);
		return Response.ok("success");
	}

	@ApiOperation(value="按照编号删除用户信息",notes="按照编号删除用户信息")
	@ApiImplicitParam(name="id",value="用户编号",paramType="path",required=true)
	@ResponseBody
	@PostMapping("/updateUser")
	public Response<String> updateAdmin(SmartUser user, HttpSession session){
		session.setAttribute("loginUser",user);
		return smartUserService.saveOrUpdate(user);
	}

	@ApiOperation(value="分页查找所有用户",notes="分页查找所有用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name="pageNum",value="当前页码",defaultValue="1"),
		@ApiImplicitParam(name="pageSize",value="每页显示数据个数(0是全部)",defaultValue="10")
	})

	@GetMapping("/userList")
	public ModelAndView userList(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
								 @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,SmartUser smartUser,ModelAndView modelAndView) {
		PageInfo<SmartUser> data= smartUserService.getAllByPage(pageNum, pageSize,smartUser);
		modelAndView.addObject("page",data);
		modelAndView.setViewName("user/user-list");
		return modelAndView;
	}

	//个人用户
	@GetMapping("/myInfo")
	public ModelAndView myInfo(HttpSession session,ModelAndView modelAndView) {
		SmartUser user=(SmartUser) session.getAttribute("loginUser");
		if(null!=user){
			SmartUser user2=smartUserService.findUserById(user.getId());
			modelAndView.addObject("list",user2);
			modelAndView.setViewName("myInfo/user-list");
			return modelAndView;
		}else{
			modelAndView.setViewName("login");
			return modelAndView;
		}
	}

	//跳转添加用户页面
	@GetMapping("/user-add")
	public ModelAndView userAddHtml(ModelAndView modelAndView) {
		modelAndView.setViewName("user/user-add");
		return modelAndView;
	}

	//跳转修改用户页面
	@GetMapping("/user-edit")
	public ModelAndView userEditHtml(Long id,ModelAndView modelAndView) {
		SmartUser user=smartUserService.findUserById(id);
		modelAndView.addObject("user",user);
		modelAndView.setViewName("user/user-edit");
		return modelAndView;
	}

	//个人信息修改
	@GetMapping("/user-update")
	public ModelAndView userUpdateHtml(Long id,ModelAndView modelAndView) {
		SmartUser user=smartUserService.findUserById(id);
		modelAndView.addObject("user",user);
		modelAndView.setViewName("myInfo/user-edit");
		return modelAndView;
	}
}
